#include "stdafx.h"
#include "APITypeDef.h"
#include "APIHook.h"
#include <vector>
#pragma comment(lib,"Ws2_32.lib")

using namespace std;
extern vector<FunAddrData >g_HookFunData;
extern ShareDllInfo *g_pShareInfo;
extern DWORD g_MyPID;



void PrintDebugString(const LPCSTR DebugStr,...)
{
	va_list marker = NULL;  
	va_start(marker, DebugStr);
	//CHAR buf[255]={0};
	size_t nLength =_vscprintf(DebugStr,marker) + 1;
	std::vector<CHAR > vBuffer(nLength, L'\0');
	int nWritten = _vsnprintf_s(&vBuffer[0], vBuffer.size(), nLength,DebugStr, marker);
	if (nWritten>0)
	{
		OutputDebugString(&vBuffer[0]);	
	}
	vBuffer.clear();
	
}


LPFUNADDRDATA GetFunAddrData(FunNameID fn)
{
	LPFUNADDRDATA retval=NULL;
	int nCount=g_HookFunData.size();
	for (int i=0;i<nCount;i++)
	{
		if (fn==g_HookFunData[i].FunName)
		{
			retval=&g_HookFunData[i];
			break;
		}
	}
	return retval;
}
DWORD GetFunAddrID(FunNameID fn)
{
	DWORD retval=-1;
	int nCount=g_HookFunData.size();
	for (int i=0;i<nCount;i++)
	{
		if (fn==g_HookFunData[i].FunName)
		{
			retval=i;
			break;
		}
	}
	return retval;
}


int WINAPI HOOKMessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uiType)
{
	LPFUNADDRDATA pfnData=GetFunAddrData(eMessageBoxW);
	if (pfnData==NULL)
	{
		return NULL;
	}
	PrintDebugString("MessageBoxW Called");
	return ((pMessageBoxW)pfnData->OldFunAddr)(hWnd,lpText,L"API HOOK DLL",uiType);
}

int PASCAL FAR HOOKsendto(IN SOCKET s,
						  IN const char FAR * buf,
						  IN int len,
						  IN int flags,
						  IN const struct sockaddr FAR *to,
						  IN int tolen)
{
	LPFUNADDRDATA pfnData=GetFunAddrData(esendto);
	if (pfnData==NULL || CAPIHook::IsNetBlocked()==TRUE)
	{
		PrintDebugString("Net Blocked");
		return WSAEHOSTUNREACH;
	}
	PrintDebugString("sendto Called");
	g_pShareInfo->AddEvent(g_MyPID,seSendto,(BYTE*)buf,len);
	int retval= ((psendto)pfnData->OldFunAddr)(s,buf,len,flags,to,tolen);
	
	return retval;
}

int PASCAL FAR HOOKsend (IN SOCKET s,
						 IN const char FAR * buf,
						 IN int len,
						 IN int flags)
{
	LPFUNADDRDATA pfnData=GetFunAddrData(esend);
	if (pfnData==NULL || CAPIHook::IsNetBlocked()==TRUE)
	{
		return WSAEHOSTUNREACH;
	}
	PrintDebugString("send Called");
	g_pShareInfo->AddEvent(g_MyPID,seSend,(BYTE*)buf,len);
	int retval= ((psend)pfnData->OldFunAddr)(s,buf,len,flags);
	return retval;

}
int PASCAL FAR HOOKlisten (IN SOCKET s,
					   IN int backlog)
{
	LPFUNADDRDATA pfnData=GetFunAddrData(elisten);
	if (pfnData==NULL || CAPIHook::IsNetBlocked()==TRUE)
	{
		return WSAEISCONN;
	}
	PrintDebugString("listen Called");
	g_pShareInfo->AddEvent((DWORD)g_MyPID,seListen,NULL,0);
	int retval= ((plisten)pfnData->OldFunAddr)(s,backlog);
	return retval;

}
int PASCAL FAR HOOKbind (IN SOCKET s,
						 IN const struct sockaddr FAR *addr,
						 IN int namelen)
{
	LPFUNADDRDATA pfnData=GetFunAddrData(ebind);
	if (pfnData==NULL || CAPIHook::IsNetBlocked()==TRUE)
	{
		return WSAEADDRNOTAVAIL;
	}
	PrintDebugString("bind Called");
	g_pShareInfo->AddEvent((DWORD)g_MyPID,sebind,(BYTE*)addr,namelen);
	int retval= ((pbind)pfnData->OldFunAddr)(s,addr,namelen);
	return retval;
}

int PASCAL FAR HOOKconnect (IN SOCKET s,
							IN const struct sockaddr FAR *name,
							IN int namelen)
{
	LPFUNADDRDATA pfnData=GetFunAddrData(econnect);
	if (pfnData==NULL || CAPIHook::IsNetBlocked()==TRUE)
	{
		return WSAEHOSTUNREACH;
	}
	sockaddr_in *scName=(sockaddr_in *)name;
	PrintDebugString("connect Called:IP:%s",inet_ntoa(scName->sin_addr));

	g_pShareInfo->AddEvent((DWORD)g_MyPID,seconnect,(BYTE*)name,namelen);

	int retval= ((pconnect)pfnData->OldFunAddr)(s,name,namelen);
	return retval;
}
int PASCAL FAR HOOKrecv (
						 IN SOCKET s,
						 __out_bcount_part(len, return) __out_data_source(NETWORK) char FAR * buf,
						 IN int len,
						 IN int flags)
{
	LPFUNADDRDATA pfnData=GetFunAddrData(erecv);
	if (pfnData==NULL || CAPIHook::IsNetBlocked()==TRUE)
	{
		return WSAEHOSTUNREACH;
	}
	PrintDebugString("recv Called:len:%d",len);

	g_pShareInfo->AddEvent((DWORD)g_MyPID,serecv,(BYTE*)buf,len);
	int retval= ((precv)pfnData->OldFunAddr)(s,buf,len,flags);
	return retval;
}


int PASCAL FAR HOOKrecvfrom (
							 IN SOCKET s,
							 __out_bcount_part(len, return) __out_data_source(NETWORK) char FAR * buf,
							 IN int len,
							 IN int flags,
							 __out_bcount(*fromlen) struct sockaddr FAR * from,
							 IN OUT int FAR * fromlen)
{
	LPFUNADDRDATA pfnData=GetFunAddrData(erecvfrom);
	if (pfnData==NULL || CAPIHook::IsNetBlocked()==TRUE)
	{
		return WSAEHOSTUNREACH;
	}
	PrintDebugString("recvfrom Called: Len:%d",len);
	g_pShareInfo->AddEvent((DWORD)g_MyPID,serecvfrom,(BYTE*)buf,len);
	int retval=((precvfrom)pfnData->OldFunAddr)(s,buf,len,flags,from,fromlen);
	return retval;
}